//=============================================================================

#include "../../inc/doSystem.h"
#include "../../inc/doLog.h"
#include "../../inc/doWorld.h"
#include "../../inc/doFilesWomb.h"

//=============================================================================

LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
{
  GLuint PixelFormat;
  PIXELFORMATDESCRIPTOR pfd =
  { sizeof(PIXELFORMATDESCRIPTOR),
    1,
    PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    16, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0,
    16, 0, 0,
    PFD_MAIN_PLANE,
    0, 0, 0, 0
  };

  switch(message)
  {
    case WM_CREATE:
    {
      SYS.hDc = GetDC(hWnd);
      PixelFormat = ChoosePixelFormat(SYS.hDc, &pfd);

      if (PixelFormat == NULL)
      {
        MessageBox(0, L"Can't find suitable pixel format.", L"Window creation error", MB_OK | MB_ICONERROR);
        LOG.alWrite("Can't find suitable pixel format.");
        PostQuitMessage(0);
      }

      if (SetPixelFormat(SYS.hDc, PixelFormat, &pfd) == NULL)
      {
        MessageBox(0, L"Can't set pixel format.", L"Window creation error", MB_OK | MB_ICONERROR);
        LOG.alWrite("Can't set pixel format.");
        PostQuitMessage(0);
      }

      SYS.glRc = wglCreateContext(SYS.hDc);

      if (SYS.glRc == NULL)
      {
        MessageBox(0, L"Can't create OpenGL context.", L"Window creation error", MB_OK | MB_ICONERROR);
        LOG.alWrite("Can't create OpenGL context.");
        PostQuitMessage(0);
      }

      if (wglMakeCurrent(SYS.hDc, SYS.glRc) == NULL)
      {
        MessageBox(0, L"Can't set OpenGL context as current.", L"Window creation error", MB_OK | MB_ICONERROR);
        LOG.alWrite("Can't set OpenGL context as current.");
        PostQuitMessage(0);
      }

      if (!SYS.asOpenGLInit())
      {
        MessageBox(0, L"Can't correctly init OpenGL.", L"OpenGL init error", MB_OK | MB_ICONERROR);
        LOG.alWrite("Can't correctly init OpenGL.");
        PostQuitMessage(0);
      }
      WORLD.awInit();

      if (!WORLD.awOpenConnection())
      {
        PostQuitMessage(0);
        LOG.alWrite("Can't connect to server");

        return 0;
      }

      WORLD.awBornWorld();
      
      break;
    }
    case WM_CLOSE:
    {
      ChangeDisplaySettings(NULL, 0);

      LOG.alWrite("============================END=============================", 0);

      wglMakeCurrent(SYS.hDc, SYS.glRc);
      wglDeleteContext(SYS.glRc);
      ReleaseDC(SYS.hWnd, SYS.hDc);
      PostQuitMessage(0);

      break;
    }
    case WM_PAINT:
    {
      SYS.asCalculateFPS();
      SYS.asPaintTheWorld();
      WORLD.awProceedPackets();
      WORLD.awWorldProcess();
      //SYS.asCheckCamera();
      SwapBuffers(SYS.hDc);

      break;
    }
    case WM_LBUTTONDOWN:
     case WM_KEYDOWN:
      {
        SYS.asKeyDown(wParam);

        break;
      }
    case WM_MOUSEMOVE:
    {
      //SYS.asCheckCamera(SYS.asGetWidth()/2, SYS.asGetHeight()/2);

      break;
    }
    default:
    {
      return DefWindowProc(hWnd, message, wParam, lParam);
    }
  }

  return 0;
}

//=============================================================================

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR cmdLine, int showCmd)
{
  MSG msg;
  WNDCLASS wc;

  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  wc.lpfnWndProc = (WNDPROC) WndProc;
  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = NULL;
  wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = NULL;
  wc.lpszMenuName = NULL;
  wc.lpszClassName = L"Client";

  SYS.asParseParameters(cmdLine);

  LOG.logPath = "client.log";
  LOG.alWrite("===========================BEGIN============================", 0);

  WOMB.afAddCategory("sources\\objects", "objects");
  WOMB.afAddCategory("sources\\characters", "characters");

  if (!RegisterClass(&wc))
  {
    LOG.alWrite("Can't register window class.");
    SYS.asPrintLastError();

    return FALSE;
  }

  SYS.hWnd = CreateWindow(wc.lpszClassName, L"Into the deepest places of the mind...", /*WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS*/WS_OVERLAPPEDWINDOW, 0, 0, SYS.asGetWidth(), SYS.asGetHeight(), NULL, NULL, hInstance, NULL);

  if (SYS.hWnd == NULL)
  {
    LOG.alWrite("Can't create window.");
    SYS.asPrintLastError();

    return FALSE;
  }

//  SYS.asSetDisplayToFullscreen();
  ShowWindow(SYS.hWnd, SW_SHOW);
  UpdateWindow(SYS.hWnd);
  SetFocus(SYS.hWnd);

  while(GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return TRUE;
}

//=============================================================================
